package com.yunhe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhe.common.QueryPageParam;
import com.yunhe.common.Result;
import com.yunhe.entity.Menu;
import com.yunhe.entity.User;
import com.yunhe.service.MenuService;
import com.yunhe.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 控制层代码
 */
@RestController
@RequestMapping("/user")
@Api(value = "/user",description = "用户信息管理")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    MenuService menuService;

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation(value = "查询所有用户",notes = "所有用户信息获取notes")
    public List<User> list(){
        List<User> userList = userService.list();
        return userList;
    }

    /**
     * 是不是根据输入的用户的账号和密码，做了一个等值的查询
     * String no,String password ，测试一下
     * @param user 是从表单接受过来的只有账号和密码的参数
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("根据账号和密码登录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "登录成功"),
            @ApiResponse(code = 404,message = "网址不对"),
            @ApiResponse(code = 500,message = "后端代码有误")
    })
    public Result login(
            @ApiParam(value = "账号和密码",required = true)
            @RequestBody User user){
        List<User> userList = userService.lambdaQuery().eq(User::getNo, user.getNo()).eq(User::getPassword, user.getPassword()).list();
        if(userList.size()>0){
            //再次定义user对象user1,是登录账号和密码之后，具备所有的信息的user对象;
            User user1 = userList.get(0);

            List<Menu> menuList = menuService.lambdaQuery().like(Menu::getMenuright, user1.getRoleId()).list();

            //将数据封装过去，从而进一步传递到前端
            HashMap res=new HashMap();
            res.put("user", user1);
            res.put("menu", menuList);

            //将封装好的res对象传递过去。
            return Result.success(res);
        }
        return Result.fail();
    }

    @PostMapping("/listPageC1")
    @ApiOperation("分页查询和模糊查询")
    public Result listPageC1(
            @ApiParam(value = "页码 每页条数 姓名 性别",required = false)
            @RequestBody QueryPageParam query){
        //得到分页查询的参数
        HashMap param=query.getParam();

        String name= (String) param.get("name");
        String sex= (String) param.get("sex");
        String roleId= (String) param.get("roleId");

        //分页,物品管理，仓库管理
        Page<User>page=new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        //查询效果:JAVA:OO语言；Rust:函数式编程，挺火；JAVA与时俱进！！！兰布达表达式;
        //查询where name like '%name% and sex='男' and role_id=1
        LambdaQueryWrapper<User>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(User::getName, name);
        }
        if(StringUtils.isNotBlank(sex))
            lambdaQueryWrapper.eq(User::getSex, sex);
        if(StringUtils.isNotBlank(roleId))
            lambdaQueryWrapper.eq(User::getRoleId, roleId);

        //调用业务层的代码;IPage
        Page<User> result = userService.page(page, lambdaQueryWrapper);

        return Result.success(result.getRecords(),result.getTotal());
    }

    @GetMapping("/findByNo")
    @ApiOperation("查询某个账号是否存在")
    public  Result findByNo(
            @ApiParam(value = "账号",required = true)
            @RequestParam String no){
        List<User> userList = userService.lambdaQuery().eq(User::getNo, no).list();
        return userList.size()>0?Result.success():Result.fail();
    }
    @PostMapping("/save")
    @ApiOperation("保存用户信息")
    public Result save(
            @ApiParam(value = "用户对象信息",required = true)
            @RequestBody User user){
        boolean result = userService.save(user);
        if(result)
            return Result.success();

        return Result.fail();
    }


    @PutMapping("/update")
    @ApiOperation("更新用户信息")
    public Result update(
            @ApiParam(value = "用户对象信息",required = true)
            @RequestBody User user){
        return userService.updateById(user)?Result.success():Result.fail();
    }

    /**
     * Integer对应的swagger类型可以是int或long
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation("根据用户id来删除用户信息")
    @ApiImplicitParam(paramType = "query",dataType = "Long",name = "id",value = "用户id",required = true)
    public Result delete(
            @ApiParam(value = "用户id",required = true)
            Integer id){
        return userService.removeById(id)?Result.success():Result.fail();
    }
}